package ch05.visitor;


import ch05.Token;
import ch05.visitor.AddNode;
import ch05.visitor.AssignNode;
import ch05.visitor.ExprNode;
import ch05.visitor.IntNode;
import ch05.visitor.MultNode;
import ch05.visitor.PrintNode;
import ch05.visitor.StatListNode;
import ch05.visitor.StatNode;
import ch05.visitor.VarNode;
import ch05.visitor.VectorNode;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static ch05.visitor.IntNode I(int i) {
        return new IntNode(new Token(Token.INT, String.valueOf(i)));
    }

    public static void main(String[] args) {
        List<StatNode> stats = new ArrayList<StatNode>();
        AddNode a = new AddNode(I(3), new Token(Token.PLUS), I(4));
        VarNode x = new VarNode(new Token(Token.ID, "x"));
        AssignNode assign = new AssignNode(x, new Token(Token.ASSIGN, "="), a);
        stats.add(assign);
        // print x * [2, 3, 4]
        Token mult = new Token(Token.MULT, "*");
        List<ExprNode> elements = new ArrayList<ExprNode>();
        elements.add(I(2));
        elements.add(I(3));
        elements.add(I(4));
        VectorNode v = new VectorNode(new Token(Token.VEC), elements);
        VarNode xref = new VarNode(new Token(Token.ID, "x"));
        ExprNode pv = new MultNode(xref, mult, v);
        PrintNode p = new PrintNode(new Token(Token.PRINT, "print"), pv);
        stats.add(p);
        StatListNode statlist = new StatListNode(stats);

        // 模式一：把访问者传入到AST中
        PrintVisitor visitor = new PrintVisitor();
        statlist.visit(visitor);

        // 模式二：把AST节点传入到访问者中
        IndependentPrintVisitor indepVisitor = new IndependentPrintVisitor();
        indepVisitor.print(statlist);
    }
}
